//
//  HJSupportInputController.m
//  KaiChat
//
//  Created by lx on 2023/12/22.
//

#import "HJSupportInputController.h"
#import "HJSupportInputBar.h"
#import "TUIDefine.h"
#import "NSString+emoji.h"
#import "HJCommonModel.h"
#import "HJGradientButton.h"
#import "HJEmojiView.h"

@interface HJSupportInputController ()<HJSupportInputBarDelegate>

@property (nonatomic, assign) CGRect keyboardFrame;
@property (nonatomic, assign) HJSupportInputStatus status;


@end

@implementation HJSupportInputController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
}

- (void)setupViews {
    self.view.backgroundColor = UIColor.whiteColor;
    _status = HJSupportInputStatusNone;
    
    _inputBar = [[HJSupportInputBar alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, TTextView_Height)];
    _inputBar.delegate = self;
    [self.view addSubview:_inputBar];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    if (_delegate && [_delegate respondsToSelector:@selector(inputController:didChangeHeight:)]){
        [_delegate inputController:self didChangeHeight:CGRectGetMaxY(self.inputBar.frame) + Bottom_SafeHeight];
    }
    if (_status == HJSupportInputStatusKeyboard) {
        _status = HJSupportInputStatusNone;
    }
}

- (void)keyboardWillShow:(NSNotification *)notification {
    _status = HJSupportInputStatusKeyboard;
}

- (void)keyboardWillChangeFrame:(NSNotification *)notification {
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    if (_delegate && [_delegate respondsToSelector:@selector(inputController:didChangeHeight:)]){
        [_delegate inputController:self didChangeHeight:keyboardFrame.size.height + CGRectGetMaxY(self.inputBar.frame)];
    }
    self.keyboardFrame = keyboardFrame;
}
 
- (void)inputBarDidTouchKeyboard:(HJSupportInputBar *)textView {
    _status = HJSupportInputStatusKeyboard;
    [_inputBar.inputTextView becomeFirstResponder];
}

- (void)inputBar:(HJSupportInputBar *)textView didChangeInputHeight:(CGFloat)offset {
    if (_delegate && [_delegate respondsToSelector:@selector(inputController:didChangeHeight:)]){
        [_delegate inputController:self didChangeHeight:self.view.frame.size.height + offset];
    }
}

- (void)inputBar:(HJSupportInputBar *)textView didSendText:(NSString *)text {
    NSString *content = [text getInternationalStringWithfaceContent];
    if(_delegate && [_delegate respondsToSelector:@selector(inputController:didSendTextMessage:)]){
        [_delegate inputController:self didSendTextMessage:content];
    }
}

- (void)inputBarDidClickFAQContentButton:(HJSupportInputBar *)textView {
    if(_delegate && [_delegate respondsToSelector:@selector(inputControllerDidClickFAQ:)]){
        [_delegate inputControllerDidClickFAQ:self];
    }
}
 
- (void)inputBarDidClickFAQButton:(HJSupportInputBar *)textView {
    if(_status == HJSupportInputStatusKeyboard){
        [self reset];
        return;
    }
    
    [_inputBar.inputTextView resignFirstResponder];
    _status = HJSupportInputStatusFAQ;
    if (_delegate && [_delegate respondsToSelector:@selector(inputController:didChangeHeight:)]){
        [_delegate inputController:self didChangeHeight:CGRectGetMaxY(self.inputBar.frame) + Bottom_SafeHeight];
    }
}
 
- (void)inputBarDidTouchClose:(HJSupportInputBar *)textView {
    [self reset];
}

- (void)inputBarDidInputAt:(HJSupportInputBar *)textView {
    if (_delegate && [_delegate respondsToSelector:@selector(inputControllerDidInputAt:)]) {
        [_delegate inputControllerDidInputAt:self];
    }
}

- (void)inputBar:(HJSupportInputBar *)textView didDeleteAt:(NSString *)atText {
    if (_delegate && [_delegate respondsToSelector:@selector(inputController:didDeleteAt:)]) {
        [_delegate inputController:self didDeleteAt:atText];
    }
}

- (void)inputBarDidDeleteBackward:(HJSupportInputBar *)textView {
    if (textView.inputTextView.text.length == 0) {
    }
}


- (void)inputTextViewShouldBeginTyping:(UITextView *)textView {
    
    if (_delegate && [_delegate respondsToSelector:@selector(inputControllerBeginTyping:)]) {
        [_delegate inputControllerBeginTyping:self];
    }
}

- (void)inputTextViewShouldEndTyping:(UITextView *)textView {
    
    if (_delegate && [_delegate respondsToSelector:@selector(inputControllerEndTyping:)]) {
        [_delegate inputControllerEndTyping:self];
    }
    
}




- (void)reset {
    if(_status == HJSupportInputStatusNone){
        return;
    }
 
    _status = HJSupportInputStatusNone;
    [_inputBar.inputTextView resignFirstResponder];
    if (_delegate && [_delegate respondsToSelector:@selector(inputController:didChangeHeight:)]){
        [_delegate inputController:self didChangeHeight:CGRectGetMaxY(self.inputBar.frame) + Bottom_SafeHeight];
    }
}
 
 

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
   
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
   
}

- (void)viewDidAppear:(BOOL)animated {
   [super viewDidAppear:animated];
   for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
       gesture.delaysTouchesBegan = NO;
   }
   self.navigationController.interactivePopGestureRecognizer.delaysTouchesBegan = NO;
}

- (void)viewDidDisappear:(BOOL)animated {
   [super viewDidDisappear:animated];
   
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
